<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        //判断闰年
        function isleadyear(year) {
            return year % 4 === 0 && year % 100 !== 0 || year % 400 === 0;
        }
        //判断月份天数
        function monthdays(year, month) {
            var days;
            switch (month) {
                case 4:
                case 6:
                case 9:
                case 11:
                    days = 30;
                    break;
                case 2:
                    days = isleadyear(year) ? 29 : 28;
                    break;
                default:
                    days = 31;
                    break;
            }
            return days;
        }
        //判断指定月份到1900-1-1的天数.
        function calcTotalDays(year, month) {
            var totaldays = 1;
            for (i = 1; i < month; i++) {
                totaldays += monthdays(year, i);
            }
            for (i = 1900; i < year; i++) {
                totaldays += isleadyear(i) ? 366 : 365;
            }
            return totaldays;
        }
        //判断指定年月是星期几
        function clacweekdays(year, month) {
            var week = calcTotalDays(year, month);
            return week = week % 7;
        }
        //判断是中文星期几
        function clacweekdays2(year, month) {
            var week = clacweekdays(year, month);
            switch (week) {
                case 0:
                    return "星期天"
                    break;
                case 1:
                    return "星期一";
                    break;
                case 2:
                    return "星期二";
                    break;
                case 3:
                    return "星期三";
                    break;
                case 4:
                    return "星期四";
                    break;
                case 5:
                    return "星期五";
                    break;
                case 6:
                    return "星期六";
                    break;
                default:
                    break;
            }
        }
        //打印日历
        function printMonthCalendar(year, month) {
            var week = "日\t一\t二\t三\t四\t五\t六\n"
            var blanks = clacweekdays(year, month);
            for (i = 0; i < blanks; i++) {
                week += "\t";
            }
            var days = monthdays(year, month);
            for (i = 1; i <= days; i++) {
                week += i + "\t";
                if ((i + blanks) % 7 === 0) {
                    week += "\n";
                }
            }
            return week;
        }

        function calendar(year, month) {
            var week = "日\t一\t二\t三\t四\t五\t六\n";
            var blanks = clacweekdays(year, month);
            for (i = 0; i < blanks; i++) {
                week += "\t";
            }
            var days = monthdays(year, month);
            for (i = 1; i <= days; i++) {
                week += i + "\t";
                if ((i + blanks) % 7 === 0) {
                    week += "\n";
                }
            }
            return week;
        }

        function calendar2(year, month) {
            var week = "日\t一\t二\t三\t四\t五\t六\n",
                blanks = clacweekdays(year, month);
            for (i = 0; i < blanks; i++) {
                week += "\t";
            }
            var days = monthdays(year, month);
            for (i = 1; i <= days; i++) {
                week += i + "\t";
                if ((i + blanks) % 7 === 0) {
                    week += "\n";
                }
            }
            return week;
        }
        document.write(isleadyear(2020) + "\n");
        document.write(monthdays(2015, 3) + "\n");
        document.write(calcTotalDays(2016, 6) + "\n");
        document.write(clacweekdays(2016, 6) + "\n");
        document.write(clacweekdays2(2016, 3) + "\n");
        console.log(printMonthCalendar(2020, 5));
        console.log(calendar(2015, 9));
        console.log(calendar2(2016, 7));
    </script>
</body>

</html>